package com.example.mail;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import com.example.MessageCodes;
import com.example.R;
import core.model.Game;
import core.model.mail.Message;
import core.util.Logger;

import java.util.ArrayList;

import static core.model.mail.Message.MessageType.BROADCAST;
import static core.model.mail.Message.MessageType.PRIVATE;


public class MailTypeActivity extends Activity {

    private MailManager mailManager;
    private Game game;

    // todo should be enum or const
    private String selectedMailType;

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case MessageCodes.Mail.LOADING_MESSAGES_COMPLETE:
                    Logger.debug("MailTypeActivity", "Mail loading was complete without any problems.");
                    showUI();
                    break;
                case MessageCodes.EXCEPTION:
                    Logger.debug("MailTypeActivity", "Mail loading was not complete.");
                    break;
            }

        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        game = (Game) getIntent().getExtras().getSerializable("game");

        mailManager = new MailManager(this, game, handler);
        mailManager.loadMessages();
    }

    private void showUI() {
        setContentView(R.layout.mailtypelayout);

        final core.model.mail.Context context = mailManager.getContext();

        TextView broadcastButton = (TextView) findViewById(R.id.showBroadcastsButton);

        broadcastButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                selectedMailType = "broadcast";
                showMessageList(BROADCAST, context);
            }
        });


        TextView privateButton = (TextView) findViewById(R.id.showPrivatesButton);

        privateButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                selectedMailType = "private";
                showMessageList(PRIVATE, context);
            }
        });
    }

    private void showMessageList(Message.MessageType messageType, core.model.mail.Context context) {
        View view = findViewById(R.id.mailTypes);
        Context viewContext = view.getContext();

        Intent i = new Intent(viewContext, MailListActivity.class);

        switch (messageType) {
            case PRIVATE:
                // todo "messages" should constant
                i.putExtra("messages", (ArrayList<Message>) context.getPrivates());
                break;

            case BROADCAST:
                // todo "messages" should constant
                i.putExtra("messages", (ArrayList<Message>) context.getBroadcasts());
                break;
        }

        i.putExtra("game", game);
        i.putExtra("selectedMailType", selectedMailType);

        viewContext.startActivity(i);
    }
}
